﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ETPHService.Entities;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace ETPHService.Helpers
{
    public static class DB
    {
        public static string ConnectionString = ConfigurationManager.ConnectionStrings["WebServiceData"].ToString();

        public static Device GetDevice(string deviceID)
        {
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM DEVICE WHERE DeviceID='" + deviceID + "'", ConnectionString);

            DataTable dt = new DataTable("Devices");

            da.Fill(dt);

            Device d = new Device();

            foreach (DataRow dr in dt.Rows)
            {
                d.Id = (Guid)dr["DeviceID"];
                d.Name = dr["Name"].ToString();
                d.SecondaryID = dr["SecondaryID"].ToString();
            }

            return d;
        }

        public static MOTD GetMOTD()
        {
            SqlDataAdapter da = new SqlDataAdapter("SELECT TOP (1) * FROM MOTD ORDER BY DATEADDED DESC", ConnectionString);

            DataTable dt = new DataTable("Devices");

            da.Fill(dt);

            MOTD d = new MOTD();

            foreach (DataRow dr in dt.Rows)
            {
                d.GU = dr["MOTD_GU"].ToString();
                d.Message = dr["Message"].ToString();
                d.DateAdded = dr["DateAdded"].ToString();
            }

            return d;
        }
    }
}